/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VenteGui.java
 *
 * Created on 21 févr. 2012, 20:54:56
 */
package app.views;

import app.App;
import app.exceptions.ReferenceOutOfStockException;
import app.models.*;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

/**
 *
 * @author Ghost
 */
public class VenteGui extends javax.swing.JPanel implements ActionListener {

    private List<ActionListener> actionListenerList = new ArrayList<ActionListener>();
    private Vente vente;
    private Vector<String> headers = new Vector<String>();
    private SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
    private JPopupMenu popMenu;
    private JMenuItem miEdit;
    private JMenuItem miDelete;
    private JMenuItem miSelect;

    /** Creates new form VenteGui */
    public VenteGui() {
        headers.add("Reference.Designation");
        headers.add("Quantite");
        headers.add("PxVente");

        initComponents();

        //Popupmenu preparation
        popMenu = new JPopupMenu("Hello");
        miEdit = new JMenuItem("Editer", App.createIcon("resources/note_edit.png"));
        miEdit.addActionListener(this);
        popMenu.add(miEdit);
        miDelete = new JMenuItem("Supprimer", App.createIcon("resources/note_delete.png"));
        miDelete.addActionListener(this);
        popMenu.add(miDelete);
        /*miSelect = new JMenuItem("Selectionner", App.createIcon("resources/note_go.png"));
        miSelect.addActionListener(this);
        popMenu.add(miSelect);*/

        tableLignesVente.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                onMouseClicked(e);
            }
        });

        setVente(null);
    }

    public void setVente(Vente v) {
        vente = v;
        if (v == null) {
            cleanup();
        } else {
            sync();
        }
    }

    private void cleanup() {
        txtClient.setText("Non fidèle");
        txtDate.setText("Pas de vente");
        tableLignesVente.clear();
        TOGGLE_BTN.setSelected(true);
        txtTotal.setText("");
        panDetails.setVisible(false);
        bgType.clearSelection();
    }

    /**
     * Synchronize ui with the vente setted
     * if vente is null, ui is reseted
     */
    public void sync() {
        if (vente == null) {
            cleanup();
        } else {
            Client cli = vente.getClient();
            if (cli == null) {
                txtClient.setText("Non fidèle");
            } else {
                txtClient.setText(cli.getPrenom() + " " + cli.getNom());
            }

            txtDate.setText(sdf.format(Calendar.getInstance().getTime()));
            TOGGLE_BTN.setSelected(!vente.getBeauTemps());

            tableLignesVente.setElements(vente.getLigneventeList());

            RB_CB.setSelected(Vente.TYPE_CB.equals(vente.getType()));
            RB_CHEQUE.setSelected(Vente.TYPE_CHEQUE.equals(vente.getType()));
            RB_ESPECES.setSelected(Vente.TYPE_ESPECES.equals(vente.getType()));
            panDetails.setVisible(Vente.TYPE_ESPECES.equals(vente.getType()));

            txtTotal.setText(String.valueOf(vente.getTotal()));
        }
    }

    public void onMouseClicked(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3) {
            Point p = e.getPoint();
            int row = tableLignesVente.rowAtPoint(p);
            tableLignesVente.setSelectedRow(row);
            popMenu.show(tableLignesVente, e.getX(), e.getY() + 20);
        }
        /*if (e.getClickCount() == 2) {
        fireReferenceSelected(getSelectedReference());
        }*/
    }

    public void addActionListener(ActionListener l) {
        actionListenerList.add(l);
    }

    public void removeActionListener(ActionListener l) {
        actionListenerList.remove(l);
    }

    protected void fireActionEvent(ActionEvent e) {
        for (ActionListener l : actionListenerList) {
            l.actionPerformed(e);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bgType = new javax.swing.ButtonGroup();
        lblDate = new javax.swing.JLabel();
        txtDate = new javax.swing.JFormattedTextField();
        lblClient = new javax.swing.JLabel();
        txtClient = new javax.swing.JTextField();
        CLIENT_BTN = new javax.swing.JButton();
        TOGGLE_BTN = new javax.swing.JToggleButton();
        lblTemps = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableLignesVente = new app.views.SimpleTable<Lignevente>();
        RB_ESPECES = new javax.swing.JRadioButton();
        RB_CB = new javax.swing.JRadioButton();
        RB_CHEQUE = new javax.swing.JRadioButton();
        panDetails = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtDonne = new javax.swing.JTextField();
        txtRendre = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        lblDate.setText("Date:");

        txtDate.setEditable(false);
        txtDate.setToolTipText("Date au format JJ/MM/AA"); // NOI18N

        lblClient.setText("Client:");

        txtClient.setText("Non fidèle");
        txtClient.setDisabledTextColor(new java.awt.Color(51, 51, 51));
        txtClient.setEnabled(false);

        CLIENT_BTN.setText("...");
        CLIENT_BTN.setToolTipText("Selectionner un client");
        CLIENT_BTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CLIENT_BTNActionPerformed(evt);
            }
        });

        TOGGLE_BTN.setText("Beau");
        TOGGLE_BTN.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                TOGGLE_BTNStateChanged(evt);
            }
        });
        TOGGLE_BTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TOGGLE_BTNActionPerformed(evt);
            }
        });

        lblTemps.setText("Temps :");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Total:");

        txtTotal.setFont(new java.awt.Font("Tahoma", 1, 12));
        txtTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtTotal.setText("0.00");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("euros");

        tableLignesVente.setPreferredSize(new java.awt.Dimension(200, 50));
        jScrollPane1.setViewportView(tableLignesVente);
        tableLignesVente.setHeaders(headers);

        bgType.add(RB_ESPECES);
        RB_ESPECES.setText("Espèces");
        RB_ESPECES.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RB_ESPECESActionPerformed(evt);
            }
        });

        bgType.add(RB_CB);
        RB_CB.setText("CB");
        RB_CB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RB_CBActionPerformed(evt);
            }
        });

        bgType.add(RB_CHEQUE);
        RB_CHEQUE.setText("Chèque");
        RB_CHEQUE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RB_CHEQUEActionPerformed(evt);
            }
        });

        jLabel3.setText("Donné:");

        txtDonne.setText("0.0");
        txtDonne.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDonneActionPerformed(evt);
            }
        });
        txtDonne.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDonneKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDonneKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtDonneKeyTyped(evt);
            }
        });

        txtRendre.setEditable(false);
        txtRendre.setText("0.0");

        jLabel4.setText("Rendre:");

        javax.swing.GroupLayout panDetailsLayout = new javax.swing.GroupLayout(panDetails);
        panDetails.setLayout(panDetailsLayout);
        panDetailsLayout.setHorizontalGroup(
            panDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panDetailsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtRendre, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                    .addComponent(txtDonne, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)))
        );
        panDetailsLayout.setVerticalGroup(
            panDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panDetailsLayout.createSequentialGroup()
                .addGroup(panDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDonne, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRendre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblDate, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDate, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblClient, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                            .addComponent(lblTemps, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtClient, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CLIENT_BTN, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(TOGGLE_BTN, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(RB_ESPECES)
                            .addComponent(RB_CB)
                            .addComponent(RB_CHEQUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2))
                            .addComponent(panDetails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDate)
                    .addComponent(txtDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CLIENT_BTN)
                    .addComponent(txtClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblClient))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TOGGLE_BTN)
                    .addComponent(lblTemps))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panDetails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(RB_ESPECES)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(RB_CB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(RB_CHEQUE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void CLIENT_BTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CLIENT_BTNActionPerformed
        fireActionEvent(evt);
    }//GEN-LAST:event_CLIENT_BTNActionPerformed

    private void TOGGLE_BTNStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_TOGGLE_BTNStateChanged
        if (!TOGGLE_BTN.isSelected()) {
            TOGGLE_BTN.setText("Beau");
        } else {
            TOGGLE_BTN.setText("Mauvais");
        }
    }//GEN-LAST:event_TOGGLE_BTNStateChanged

    private void TOGGLE_BTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TOGGLE_BTNActionPerformed
        fireActionEvent(evt);
    }//GEN-LAST:event_TOGGLE_BTNActionPerformed

    private void RB_CBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RB_CBActionPerformed
        if (panDetails.isVisible()) {
            panDetails.setVisible(false);
        }
        fireActionEvent(evt);
    }//GEN-LAST:event_RB_CBActionPerformed

    private void RB_ESPECESActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RB_ESPECESActionPerformed
        if (!panDetails.isVisible()) {
            panDetails.setVisible(true);
        }
        fireActionEvent(evt);
    }//GEN-LAST:event_RB_ESPECESActionPerformed

    private void RB_CHEQUEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RB_CHEQUEActionPerformed
        if (panDetails.isVisible()) {
            panDetails.setVisible(false);
        }
        fireActionEvent(evt);
    }//GEN-LAST:event_RB_CHEQUEActionPerformed

    private void txtDonneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDonneKeyTyped
        //calcul();
    }//GEN-LAST:event_txtDonneKeyTyped

    private void txtDonneKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDonneKeyReleased
        calcul();
    }//GEN-LAST:event_txtDonneKeyReleased

    private void txtDonneKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDonneKeyPressed
        //calcul();
    }//GEN-LAST:event_txtDonneKeyPressed

    private void txtDonneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDonneActionPerformed
        txtDonne.setSelectionStart(0);
        txtDonne.setSelectionEnd(txtDonne.getText().length() - 1);
    }//GEN-LAST:event_txtDonneActionPerformed

    @Override
    public void addMouseListener(MouseListener l) {
        tableLignesVente.addMouseListener(l);
    }

    @Override
    public void removeMouseListener(MouseListener l) {
        tableLignesVente.removeMouseListener(l);
    }

    private void calcul() {
        try {
            txtRendre.setToolTipText("");
            txtRendre.setText("calcul...");
            Double total = Double.parseDouble(txtTotal.getText());
            Double donne = Double.parseDouble(txtDonne.getText());
            if (total > donne) {
                txtRendre.setText("Pas assez!");
            } else {
                txtRendre.setText(String.valueOf(donne - total));
            }
        } catch (Exception e) {
            txtRendre.setText("Calc. Err.");
            txtRendre.setToolTipText("T'as fais de la merde dans la saisie non?");
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JButton CLIENT_BTN;
    public static javax.swing.JRadioButton RB_CB;
    public static javax.swing.JRadioButton RB_CHEQUE;
    public static javax.swing.JRadioButton RB_ESPECES;
    public static javax.swing.JToggleButton TOGGLE_BTN;
    private javax.swing.ButtonGroup bgType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblClient;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblTemps;
    private javax.swing.JPanel panDetails;
    private app.views.SimpleTable<Lignevente> tableLignesVente;
    private javax.swing.JTextField txtClient;
    private javax.swing.JFormattedTextField txtDate;
    private javax.swing.JTextField txtDonne;
    private javax.swing.JTextField txtRendre;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables

    /**
     * @todo sortir cette methode vers la classe NewVentePan ainsi que la 
     *  gestion du popupmenu
     * @param e 
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        //Editing a ligneVente
        if (source.equals(miEdit)) {
            Lignevente lv = tableLignesVente.getSelectedElement();
            if (lv != null) {
                //App.displayMessage("Modification de la ligne : " + lv, App.MESSAGE_TYPE.INFO);
                int oldQte = lv.getQuantite();
                LigneVenteDialog dial = new LigneVenteDialog(lv);
                dial.showDialog();
                int newQte = lv.getQuantite();

                Lignevente oldLv = lv.clone();

                if (newQte == 0) {
                    vente.getLigneventeList().remove(lv);
                    lv.cancel();
                } else if (newQte != oldQte) {
                    int diff = newQte - oldQte;
                    try {
                        App.getInstance().getCatalogue().pickReference(lv.getReference(), diff);
                    } catch (ReferenceOutOfStockException ex) {
                        App.displayMessage("Pas assez en stock. Annulation de la modification.", App.MESSAGE_TYPE.ERROR);
                        lv = oldLv;
                    }
                }
                sync(); // refresh
            } else {
                App.displayMessage("Veuillez selectionner une ligne à "
                        + "modifier svp!", App.MESSAGE_TYPE.ERROR);
                return;
            }
        } else if(source.equals(miDelete)){
            Lignevente lv = tableLignesVente.getSelectedElement();
            if(lv != null){
                if(JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(this, 
                        "Êtes vous sûr de vouloir supprimer la ligne "
                        + "selectionnée?", "Supprimer une ligne", 
                        JOptionPane.YES_NO_OPTION)){
                    
                    vente.getLigneventeList().remove(lv);
                    lv.cancel();
                    App.displayMessage("Ligne supprimée!", App.MESSAGE_TYPE.INFO);
                    sync(); // refresh
                }
            } else {
                //TODO error message
            }
        }
    }
}
